$(function(){
	var basePath=$("#basepath").val();
	//筛选按钮
	$("#departmentFilter_go").click(function(){
		var clgId=$("#departmentFilter_form select[name='college']").val();
		window.location=basePath+"admin/department/showDepartmentList/"+clgId+"/1";
	});
	//编辑按钮
	$(".department_edit").click(function(){
		var depId=$(this).attr("data-departmentId");
		$("#department_edit_box_alert_container").html("");//清空提示框
		$.get(basePath+ "admin/department/getDepartment/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						var collegeId=data.college.id;
						$("#department_edit_box_form input[name='department_no']").val(data.no);
						$("#department_edit_box_form input[name='department_name']").val(data.name);
						$("#department_edit_box_form input[name='department_id']").val(data.id);
						$("#department_edit_box_form select[name='college']").val(collegeId);
						$("#department_edit_box .ajax_wait").hide();
						$("#department_edit_box_form").show();
					}
		},"json");
	});
	//编辑->保存
	$("#department_edit_box_save").click(function(){
		var clgId=$("#department_edit_box_form select[name='college']").val();
		var depId =$("#department_edit_box_form input[name='department_id']").val();
		var depNo =$("#department_edit_box_form input[name='department_no']").val();
		var depName =$("#department_edit_box_form input[name='department_name']").val();
		$("#department_edit_box_alert_container .empty_field_error").alert('close');
		$("#department_edit_box_alert_container .repeat_no_error").alert('close');
		$("#department_edit_box_alert_container .repeat_name_error").alert('close');
		$.post(basePath+ "admin/department/updateDepartment",{id:depId,no:depNo,name:depName,collegeId:clgId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-3":
							makeAlert("字段不允许为空,请重新检查","error",$("#department_edit_box_alert_container"),"empty_field_error");
							break;
						case "-4":
							makeAlert("已存在编号为"+depNo+"的系,请检查后再输入","error",$("#department_edit_box_alert_container"),"repeat_no_error");
							break;
						case "-5":
							makeAlert("已存在名为"+depName+"的系,请检查后再输入","error",$("#department_edit_box_alert_container"),"repeat_name_error");
							break;
						case "-6":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							$("#department_edit_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//删除按钮
	$(".department_delete").click(function(){
		$("#department_delete_box input[name='departmentId']").val($(this).attr("data-departmentId"));
	});
	//删除确认
	$("#department_delete_box_delete").click(function(){
		var depId=$("#department_delete_box input[name='departmentId']").val();
		$.get(basePath+ "admin/department/deleteDepartment/"+depId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							noty({text:"删除错误:此系之下还有未删除的教师",type:"error"});
							$("#department_delete_box").modal('hide');
							break;
						case "-3":
							noty({text:"删除错误:此系之下还有未删除的专业",type:"error"});
							$("#department_delete_box").modal('hide');
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#profession_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//添加按钮
	$("#department_add").click(function(){
		$("#department_add_box_alert_container").html("");
	});
	//添加确认
	$("#department_add_box_save").click(function(){
		var clgId=$("#department_add_box_form select[name='college']").val();
		var depNo =$("#department_add_box_form input[name='department_no']").val();
		var depName =$("#department_add_box_form input[name='department_name']").val();
		$("#department_add_box_alert_container .empty_field_error").alert('close');
		$("#department_add_box_alert_container .repeat_no_error").alert('close');
		$("#department_add_box_alert_container .repeat_name_error").alert('close');
		$.post(basePath+ "admin/department/insertDepartment",{no:depNo,name:depName,collegeId:clgId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#department_add_box_alert_container"),"empty_field_error");
							break;
						case "-3":
							makeAlert("已存在编号为"+depNo+"的系,请检查后再输入","error",$("#department_add_box_alert_container"),"repeat_no_error");
							break;
						case "-4":
							makeAlert("已存在名为"+depName+"的系,请检查后再输入","error",$("#department_add_box_alert_container"),"repeat_name_error");
							break;
						case "-5":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							$("#department_add_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});